package com.byzx.wine.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzx.wine.dao.SyroleMapper;
import com.byzx.wine.dao.SyuserroleMapper;
import com.byzx.wine.jdbcdao.RoleDao;
import com.byzx.wine.po.Syrole;
import com.byzx.wine.po.Syuserrole;
import com.byzx.wine.service.IRoleService;
import com.byzx.wine.util.Page;

@Service
public class RoleService implements IRoleService {
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private SyroleMapper syroleMapper;
	@Autowired
	private SyuserroleMapper syuserroleMapper;

	
	public int deleteByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return syroleMapper.deleteByPrimaryKey(id);
	}

	
	public int insert(Syrole record) {
		// TODO Auto-generated method stub
		return syroleMapper.insert(record);
	}


	public int updateByPrimaryKey(Syrole record) {
		// TODO Auto-generated method stub
		return syroleMapper.updateByPrimaryKey(record);
	}


	public Page<Map<String, Object>> findPage(Page<Map<String, Object>> page) {
		String sqlid = "com.byzx.wine.jdbcdao.RoleDao.SelectAll";
		return roleDao.findPage(sqlid, page);
	}

	public boolean deleteAll(String ids) {
		try {
			String[] roles =ids.split(",");
			for(int i =0;i<roles.length;i++){
				
				int id =Integer.parseInt(roles[i]);
				syroleMapper.deleteByPrimaryKey(id);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}


	public Map<String, Object> selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return syroleMapper.selectByPrimaryKey(id);
	}


	@Override
	public List<Map<String, Object>> findRole() {
		// TODO Auto-generated method stub
		return syroleMapper.findRole();
	}


	@Override
	public Integer dealSyuserrole(Integer userid, String ids) {
		Integer back = 0;
		Syuserrole record = new Syuserrole();
		record.setUserid(userid);
		syuserroleMapper.deleteSelective(record);
		String [] str = ids.split(",");
		for(int i=0;i<str.length;i++){
			Syuserrole record1 = new Syuserrole();
			record1.setUserid(userid);
			record1.setRoleid(Integer.parseInt(str[i]));
			back += syuserroleMapper.insertSelective(record1);
		}
		return back;
	}
}
